package com.ygp.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * <Description> <br>
 *
 * @author linmiaolai@sanyygp.com<br>
 * @version 1.0<br>
 * @date 2021/08/30 <br>
 */
public class Counter {
    private int i;
    private AtomicInteger atomicI = new AtomicInteger(0);

    public void count() {
        i++;
    }

    public int getI() {
        return i;
    }

    public void safeCount() {
        for (; ; ) {
            int i = atomicI.get();
            boolean suc = atomicI.compareAndSet(i, ++i);
            if (suc) {
                break;
            }
        }
    }

    public AtomicInteger getAtomicI() {
        return atomicI;
    }
}
